package com.lry.pda.bottom.c.backend.code;

import com.lry.pda.bottom.c.frontend.CTokenType;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CodeNodeImpl implements CodeNode{

    private CTokenType type;
    private CodeNode parent;
    private List<CodeNode> children;
    private String name;
    private Map<CodeKey, Object> codeKeyMap = new HashMap<>();

    public CodeNodeImpl(CTokenType type) {
        this.type = type;
        this.parent = null;
        this.children = new ArrayList<>();
        setAttr(CodeKey.TokenType, type);
    }

    @Override
    public CodeNode getParent() {
        return parent;
    }

    @Override
    public void addChild(CodeNode child) {
        if(null!=child){
            this.children.add(0,child);
            ((CodeNodeImpl)child).parent = this;
        }
    }

    @Override
    public List<CodeNode> getChildren() {
        return children;
    }

    @Override
    public void setAttr(CodeKey key, Object val) {
        if(key==CodeKey.TEXT){
            this.name = (String) val;
        }
        if(null==val){
            return;
        }
        codeKeyMap.put(key,val);
    }

    @Override
    public Object getAttr(CodeKey key) {
        return codeKeyMap.get(key);
    }
//    @Override
//    public String toString() {
//        String info = "";
//        if (get(CodeKey.VALUE) != null) {
//            info += "Node Value is " + get(CodeKey.VALUE).toString();
//        }
//
//        if (get(CodeKey.TEXT) != null) {
//            info += "\nNode Text is " + get(CodeKey.TEXT).toString();
//        }
//
//        if (get(CodeKey.SYMBOL) != null) {
//            info += "\nNode Symbol is " + get(CodeKey.SYMBOL).toString();
//        }
//        return info  + "\n Node Type is " + type.toString();
//    }


}
